﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

public static class ListExtend
{

    /// <summary>
    /// 将某个列表转化为另一个类型的列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="E"></typeparam>
    /// <param name="thisList"></param>
    /// <param name="getElement">参数是索引</param>
    /// <returns></returns>
    public static List<E> GetListWithIndex<T, E>(this List<T> thisList, Func<int, E> getElement)
    {
        List<E> ele = new List<E>();

        for (int i = 0; i < thisList.Count; i++)
        {
            ele.Add(getElement(i));
        }

        //thisList.ForEach(t=>ele.Add(getElement?.Invoke(T)))

        return ele;
    }
    /// <summary>
    /// 将某个列表转化为另一个类型的列表
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <typeparam name="E"></typeparam>
    /// <param name="thisList"></param>
    /// <param name="getElement">参数是索引</param>
    /// <returns></returns>
    public static List<E> GetList<T, E>(this List<T> thisList, Func<T, E> getElement)
    {
        if (getElement == null) return default;

        List<E> ele = new List<E>();

        //for (int i = 0; i < thisList.Count; i++)
        //{
        //    ele.Add(getElement(i));
        //}

        thisList.ForEach(t => ele.Add(getElement(t)));

        return ele;
    }

    public static void ForEachIndex<T>(this List<T> thisList, Action<int> action)
    {
        for (int i = 0; i < thisList.Count; i++)
        {
            action?.Invoke(i);
        }
    }

    public static int Max<T>(this IList<T> source, Func<T, int> selector, out int index) => (int)source.Max(t => (float)selector(t), out index);

    public static int Min<T>(this IList<T> source, Func<T, int> selector, out int index) => (int)source.Min(t => (float)selector(t), out index);

    public static float Max<T>(this IList<T> source, Func<T, float> selector, out int index)
    {
        float maxVal = float.MinValue;
        index = -1;
        for (int i = 0; i < source.Count; i++)
        {
            float v = selector(source[i]);
            if (v > maxVal)
            {
                maxVal = v;
                index = i;
            }
        }
        return maxVal;
    }
    public static float Min<T>(this IList<T> source, Func<T, float> selector, out int index)
    {
        float minVal = float.MaxValue;
        index = -1;
        for (int i = 0; i < source.Count; i++)
        {
            float v = selector(source[i]);
            if (v < minVal)
            {
                minVal = v;
                index = i;
            }
        }
        return minVal;
    }

    /// <summary>
    /// 删除最后一个值等于t的节点后面的节点
    /// </summary>
    public static void RemoveAfter<T>(this LinkedList<T> source, T t)
    {
        if (!source.Contains(t)) return;

        while (!source.Last.Value.Equals(t))
        {
            source.RemoveLast();
        }
    }

    /// <summary>
    /// 去除重复
    /// </summary>
    public static List<E> Distinct<E>(this List<E> source)
    {
        return source.Where((x, x_index) => x_index == source.FindIndex(y => y.Equals(x))).ToList();
    }
}

